// !面试技巧
<template>
  <div class="articles-container">
    <el-card>
      <!-- 筛选 -->
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input style="200px" v-model="keyword"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="state">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="text-align:right">
              <el-button @click="clear()">清除</el-button>
              <el-button @click="search()" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align:right">
          <el-button
            icon="el-icon-edit"
            @click="openDialog()"
            type="success"
            size="small"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>
      <!-- 数据记录 -->
      <el-alert
        :title="`数据一共 ${counts}条`"
        style="margin-bottom:15px"
        type="info"
        class="alert"
        :closable="false"
        show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table :data="articles">
        <el-table-column
          label="序号"
          type="index"
          width="80px"
        ></el-table-column>
        <el-table-column label="文章标题" width="400px">
          <template slot-scope="scope">
            {{ scope.row.title }}
            <a
              href="#"
              class="el-icon-film"
              v-if="scope.row.videoURL"
              @click.prevent="openVideo(scope.row.videoURL)"
            ></a>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"></el-table-column>
        <el-table-column label="录入时间">
          <!-- 时间过滤器直接调用'@/filters/index.js' -->
          <template slot-scope="scope">{{
            scope.row.createTime | parseTimeByString
          }}</template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template slot-scope="scope">{{
            scope.row.state === 1 ? '已启用' : '已禁用'
          }}</template>
        </el-table-column>
        <!-- 操作按钮 -->
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 预览:父传子 -->
            <el-button type="text" @click="previewArticle(scope.row)"
              >预览</el-button
            >
            <el-button type="text" @click="hToggleState(scope.row)">{{
              scope.row.state === 1 ? '禁用' : '启用'
            }}</el-button>
            <!-- 文章状态'已启用'时,2个按钮禁用disabled  -->
            <el-button
              type="text"
              @click="openDialog(scope.row)"
              :disabled="scope.row.state === 1"
              >修改</el-button
            >
            <el-button
              type="text"
              @click="hDelArticle(scope.row)"
              :disabled="scope.row.state === 1"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        style="margin-top:20px;text-align:right"
        background
        layout="prev, pager, next, sizes, jumper"
        :total="counts"
        :page-size="pagesize"
        :current-page="page"
        :page-sizes="[5, 10, 20, 50]"
        @current-change="changePage"
        @size-change="changeSize"
      ></el-pagination>
    </el-card>
    <!-- 视频弹框 -->
    <div class="video-preview" v-if="videoURL">
      <div class="close" @click="closeVideo()">
        <span class="el-icon-close"></span>
      </div>
      <div class="box">
        <video ref="video" :src="videoURL"></video>
      </div>
    </div>
    <!-- 预览弹框 -->
    <articles-preview
      ref="articlesPreview"
      :data="currArticle"
    ></articles-preview>
    <!-- 新增/修改弹框 -->
    <articles-add
      ref="articlesAdd"
      :data="currArticle"
      @updateList="updateList()"
    ></articles-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import ArticlesAdd from '../components/articles-add'
import ArticlesPreview from '../components/articles-preview'
export default {
  name: 'Articles',
  // 注册组件
  components: { ArticlesAdd, ArticlesPreview },
  data() {
    return {
      articles: [], // 文章列表
      counts: null, // 总条数
      keyword: null, // 搜索关键字
      state: null, // 文章状态
      page: 1,
      pagesize: 10,
      currArticle: {}, // 当前文章数据(预览/添加/修改)
      videoURL: null // 视频路径
    }
  },
  mounted() {
    this.hGetArticlesList()
  },
  methods: {
    // 获取文章列表
    hGetArticlesList() {
      const params = {
        keyword: this.keyword,
        state: this.state,
        page: this.page,
        pagesize: this.pagesize
      }
      list(params).then(res => {
        console.log(res)
        this.articles = res.data.items
        this.counts = res.data.counts
      })
    },
    // 切换文章状态
    hToggleState(data) {
      const params = {
        id: data.id,
        state: data.state === 1 ? 0 : 1
      }
      changeState(params).then(res => {
        console.log(res)
        this.$message.success('操作成功')
        data.state = data.state === 1 ? 0 : 1
      })
    },
    // 删除文章
    hDelArticle(data) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          remove(data).then(res => {
            console.log(res)
            this.$message.success('删除成功')
            // 重新渲染页面
            this.hGetArticlesList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 筛选搜索
    search() {
      const params = {
        keyword: this.keyword,
        state: this.state,
        page: 1,
        pagesize: 10
      }
      list(params).then(res => {
        console.log(res)
        this.articles = res.data.items
        this.counts = res.data.counts
      })
    },
    // 筛选清除
    clear() {
      this.keyword = null
      this.state = null
      this.page = 1
      this.pagesize = 10
      this.hGetArticlesList()
    },
    // 开启视频弹框
    openVideo(url) {
      // 点击视频路径地址
      this.videoURL = url
      // 在数据变化后要执行某个操作，而这个操作需要使用随数据改变而改变的DOM结构时，这个操作都应该放进Vue.nextTick()的回调函数中。
      this.$nextTick(() => {
        this.$refs.video.play()
      })
    },
    // 关闭视频弹框
    closeVideo() {
      this.$refs.video.pause()
      this.videoURL = null
    },
    // 预览文章弹框
    previewArticle(article) {
      console.log(article)
      this.currArticle = article
      this.$nextTick(() => {
        // 预览弹框
        this.$refs.articlesPreview.open()
      })
    },
    // 新增+修改弹框
    openDialog(article = {}) {
      this.currArticle = article
      this.$nextTick(() => {
        this.$refs.articlesAdd.open()
      })
    },
    // 新增/修改后重新渲染页面
    updateList() {
      if (!this.currArticle.id) {
        this.page = 1
      }
      this.hGetArticlesList()
    },
    // 切换页码
    changePage(num) {
      this.page = num
      this.hGetArticlesList()
    },
    // 切换pagesize
    changeSize(size) {
      this.page = 1
      this.pagesize = size
      this.hGetArticlesList()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
</style>

<style lang="scss" scoped>
.articles-container {
  padding: 10px;
  .el-icon-film {
    color: blue;
    font-size: 18px;
  }
}
.video-preview {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .close {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 30px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}
</style>
